package com.attendance.www.hr.service.impl;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import com.attendance.www.hr.beans.SpecialBean;
import com.attendance.www.hr.beans.SpecialPageBean;
import com.attendance.www.hr.dao.mapper.AttendanceMapper;
import com.attendance.www.hr.dao.mapper.ClosedMapper;
import com.attendance.www.hr.dao.mapper.EmployeeSpecialMapper;
import com.attendance.www.hr.dao.mapper.RequestMapper;
import com.attendance.www.hr.dao.mapper.SpecialExtMapper;
import com.attendance.www.hr.dao.mapper.SpecialMapper;
import com.attendance.www.hr.entitys.Attendance;
import com.attendance.www.hr.entitys.AttendanceExample;
import com.attendance.www.hr.entitys.Closed;
import com.attendance.www.hr.entitys.ClosedExample;
import com.attendance.www.hr.entitys.EmployeeSpecialExample;
import com.attendance.www.hr.entitys.EmployeeSpecialKey;
import com.attendance.www.hr.entitys.Special;
import com.attendance.www.hr.entitys.SpecialExample;
import com.attendance.www.hr.exception.ResultFaultException;
import com.attendance.www.hr.service.IApprovalService;
import com.attendance.www.hr.service.ICommonService;
import com.attendance.www.hr.service.ISpecialService;

@Service
public class SpecialServiceImpl implements ISpecialService {
	@Resource
	private SpecialExtMapper specialExtDao;
	@Resource
	private SpecialMapper specialDao;
	@Resource
	private EmployeeSpecialMapper employeeSpecialDao;
	@Resource
	private AttendanceMapper attendanceDao;
	@Resource
	private ICommonService commonService;
	@Resource
	private RequestMapper requestDao;
	@Resource
	private IApprovalService approvalService;
	@Resource
	private ClosedMapper closedDao;

	/**
	 * 获取全部特殊情况信息
	 */
	@Override
	public SpecialPageBean getSpecialList(SpecialBean specialBean) {
		SpecialPageBean specialPageBean=new SpecialPageBean();
		List<SpecialBean> specialBeanList=specialExtDao.getSpecialPageList(specialBean);
		specialPageBean.setSpecialBeanList(specialBeanList);
		int pageTotal=specialExtDao.getSpecialListCount(specialBean);
		specialPageBean.setPageTotal(pageTotal);
		return specialPageBean;
	}

	/**
	 * 获取特殊考勤人员ID列表数据
	 */
	@Override
	public SpecialBean getEmployeeIdListBySpecialId(Integer specialId) {
		SpecialBean specialBean=new SpecialBean();
		List<Integer> employeeIdList = specialExtDao.getEmployeeIdListBySpecialId(specialId);
		specialBean.setEmployeeIdList(employeeIdList);
		//根据日期查询，考勤记录日期是否生成数据
		Special special=specialDao.selectByPrimaryKey(specialId);
		if(special!=null){
			AttendanceExample example=new AttendanceExample();
			example.createCriteria().andAttendanceDateEqualTo(special.getSpecialDate());
			List<Attendance> attendanceList=attendanceDao.selectByExample(example);
			if(attendanceList.size()>0){
				specialBean.setSpecialDateType(true);
			}else{
				specialBean.setSpecialDateType(false);
			}
		}
		return specialBean;
	}

	/**
	 * 新增验证--zx修改
	 */
	@Override
	public boolean verifySaveSpecial(SpecialBean specialbean){
		SpecialExample specialExample=new SpecialExample();
		specialExample.createCriteria().andSpecialDateEqualTo(specialbean.getSpecialDate());
		List<Special> specialList=specialDao.selectByExample(specialExample);
		if (specialList.size()>0) {
			for (Special special : specialList) {
				if (specialbean.getEmployeeIdList()!=null && specialbean.getEmployeeIdList().size()>0) {
					EmployeeSpecialExample example=new EmployeeSpecialExample();
					example.createCriteria().andSpecialIdEqualTo(special.getSpecialId())
					.andEmployeeIdIn(specialbean.getEmployeeIdList());
					List<EmployeeSpecialKey> employeeSpecialList = employeeSpecialDao.selectByExample(example);
					if(employeeSpecialList.size()>0){
						return true;
					}
				}
			}
		}
		return false;
	}
	
	/**
	 * 修改，删除验证
	 * 修改的数据，原来的日期，设置了特殊情况，那天是否已生成了考勤
	 */
	@Override
	public boolean verifyUpdateSpecial(SpecialBean specialBean) {
		Integer specialId = specialBean.getSpecialId();
		Special special = specialDao.selectByPrimaryKey(specialId);
		Date specialDate = special.getSpecialDate();
		
		EmployeeSpecialExample esExample=new EmployeeSpecialExample();
		esExample.createCriteria().andSpecialIdEqualTo(specialId);				
		List<EmployeeSpecialKey> employeeSpecialKeyList=employeeSpecialDao.selectByExample(esExample);
		List<Integer> employeeIdList=new ArrayList<>();
		if (employeeSpecialKeyList.size()>0) {
			for (EmployeeSpecialKey employeeSpecialKey : employeeSpecialKeyList) {
				employeeIdList.add(employeeSpecialKey.getEmployeeId());
			}
		}
		if (employeeIdList.size()>0) {
			AttendanceExample example=new AttendanceExample();
			example.createCriteria().andAttendanceDateEqualTo(specialDate)
			.andEmployeeIdIn(employeeIdList);
			List<Attendance> list = attendanceDao.selectByExample(example);
			if(list.size()>0){
				return true;
			}
		}
		return false;
	}

	/**
	 * 新增特殊情况
	 */
	@Override
	public boolean saveSpecial(SpecialBean specialBean) {
		HashMap<EmployeeSpecialKey, EmployeeSpecialKey> map = new HashMap<EmployeeSpecialKey, EmployeeSpecialKey>();
		Special special = new Special();
		Date spDate=specialBean.getSpecialDate();
		List<Integer> employeeIdList = specialBean.getEmployeeIdList();
		//早上特殊时间
		String startTimeSp=specialBean.getStartTime();
		//晚上特殊时间
		String endTimeSp=specialBean.getEndTime();
		
		special.setSpecialDate(spDate);
		special.setSpecialName(specialBean.getSpecialName());
		special.setDescription(specialBean.getDescription());
		special.setStartTime(startTimeSp);
		special.setEndTime(endTimeSp);
		special.setEmployeeId(specialBean.getEmployeeId());//管理员ID
		int record = specialDao.insertSelective(special);
		if (record<=0) {
			return false;
		}
		//循环添加添加人员
		for(int i=0;i<specialBean.getEmployeeIdList().size();i++){
			EmployeeSpecialKey employeeSpecials=new EmployeeSpecialKey();
			employeeSpecials.setSpecialId(special.getSpecialId());
			employeeSpecials.setEmployeeId(specialBean.getEmployeeIdList().get(i));
			if(map.get(employeeSpecials)==null) {
				employeeSpecialDao.insertSelective(employeeSpecials);
				map.put(employeeSpecials, employeeSpecials);
			}
		}
		if (employeeIdList.size()>0) {
			//重新计算考勤状态 
			approvalService.resetAttendance(spDate, spDate, employeeIdList);
		}
		return true;
	}

	/**
	 * 修改特殊考勤数据
	 */
	@Override
	public boolean updateSpecial(SpecialBean specialBean) {
		HashMap<EmployeeSpecialKey, EmployeeSpecialKey> map = new HashMap<EmployeeSpecialKey, EmployeeSpecialKey>();
		//可减少员工，可改日期，可以加员工，可以改时间----相对的
		int specialId=specialBean.getSpecialId();
		//验证数据存在
		Special specialDB = specialDao.selectByPrimaryKey(specialId);
		if (specialDB==null) {
			throw new ResultFaultException("0_00012");
		}
		//验证信息是否被修改
		if(!specialDB.getUpdateTime().equals(specialBean.getUpdateTime())){
			throw new ResultFaultException("0_00011");
		}
		Date spDate=specialBean.getSpecialDate();
		String startTimeSp=specialBean.getStartTime();
		String endTimeSp=specialBean.getEndTime();
		//修改special特殊情况信息
		Special special = new Special();
		special.setSpecialId(specialId);
		special.setSpecialDate(spDate);
		special.setSpecialName(specialBean.getSpecialName());
		special.setDescription(specialBean.getDescription());
		special.setStartTime(startTimeSp);
		special.setEndTime(endTimeSp);
		special.setEmployeeId(specialBean.getEmployeeId());//管理员ID
		int record = specialDao.updateByPrimaryKeySelective(special);
		if (record<=0) {
			return false;
		}else {
			if(StringUtils.isBlank(special.getStartTime())) {
				special.setStartTime(null);
			}
			if(StringUtils.isBlank(special.getEndTime())) {
				special.setEndTime(null);
			}
			specialExtDao.updateSpecialBySpecialId(special);
		}
		//修改后，先清关系表--删除员工和特殊情况关系数据，再添加
		EmployeeSpecialExample example=new EmployeeSpecialExample();
		example.createCriteria().andSpecialIdEqualTo(specialId);
		employeeSpecialDao.deleteByExample(example);
		//循环添加添加人员
		for(int i=0;i<specialBean.getEmployeeIdList().size();i++){
			EmployeeSpecialKey employeeSpecials=new EmployeeSpecialKey();
			employeeSpecials.setSpecialId(special.getSpecialId());
			employeeSpecials.setEmployeeId(specialBean.getEmployeeIdList().get(i));
			if(map.get(employeeSpecials)==null) {
				employeeSpecialDao.insertSelective(employeeSpecials);
				map.put(employeeSpecials, employeeSpecials);
			}
		}
		if (specialBean.getEmployeeIdList()!=null && specialBean.getEmployeeIdList().size()>0) {
			//重新计算考勤状态 
			approvalService.resetAttendance(spDate, spDate, specialBean.getEmployeeIdList());
		}
		//判断如果特殊日期没有生效时，可以删除数据，重新插入
		/*AttendanceExample attendanceExample=new AttendanceExample();
		attendanceExample.createCriteria().andAttendanceDateEqualTo(special.getSpecialDate());
		List<Attendance>  attendanceList=attendanceDao.selectByExample(attendanceExample);*/
//		}else{
//			//循环添加添加人员
//			for(int i=0;i<specialBean.getEmployeeIdList().size();i++){
//				//判断员工和生效日期是否已经存在，不存在插入新的员工和特殊情况关系
//				attendanceExample=new AttendanceExample();
//				attendanceExample.createCriteria()
//				.andAttendanceDateEqualTo(special.getSpecialDate())
//				.andEmployeeIdEqualTo(specialBean.getEmployeeIdList().get(i));
//				attendanceList=attendanceDao.selectByExample(attendanceExample);
//				if(attendanceList.size()<=0){
//					EmployeeSpecial employeeSpecials=new EmployeeSpecial();
//					employeeSpecials.setSpecialId(special.getSpecialId());
//					employeeSpecials.setEmployeeId(specialBean.getEmployeeIdList().get(i));
//					employeeSpecialDao.insertSelective(employeeSpecials);
//				}
//				//处理特殊情况异常操作考勤记录表
//			}
//		}
		return true;
	}

	/**
	 * 删除特殊考勤数据
	 */
	@Override
	public boolean deleteSpecial(Integer specialId) {
		// 物理删除
		int record = specialDao.deleteByPrimaryKey(specialId);
		if (record <= 0) {
			return false;
		}
		EmployeeSpecialExample example=new EmployeeSpecialExample();
		example.createCriteria().andSpecialIdEqualTo(specialId);
		record = employeeSpecialDao.deleteByExample(example);
		if (record <= 0) {
			return false;
		}
		return true;
	}
	/**
	 * 验证该日期是否封账
	 */
	@Override
	public boolean verifyCloseAccount(SpecialBean specialBean) {
		//验证修改后的日期是不是再封账期里
		Date selectDate=specialBean.getSpecialDate();
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(selectDate);
		int year = calendar.get(java.util.Calendar.YEAR);
		int month = calendar.get(java.util.Calendar.MONTH) + 1;
		ClosedExample example=new ClosedExample();
		example.createCriteria().andClosedMonthEqualTo(month).andClosedYearEqualTo(year);
		List<Closed> closedList=closedDao.selectByExample(example);
		
		//验证修改前的日期是不是再封账期里
		ClosedExample exampleBefore=new ClosedExample();
		List<Closed> closedBeforeList=new ArrayList<Closed>();
		if(specialBean.getSpecialId()!=null) {
		int specialId=specialBean.getSpecialId();
		Special specialBefore = specialDao.selectByPrimaryKey(specialId);
		Date selectDateBefore=specialBefore.getSpecialDate();
		Calendar calendarBefore = Calendar.getInstance();
		calendarBefore.setTime(selectDateBefore);
		int yearBefore = calendarBefore.get(java.util.Calendar.YEAR);
		int monthBefore = calendarBefore.get(java.util.Calendar.MONTH) + 1;
		exampleBefore.createCriteria().andClosedMonthEqualTo(monthBefore).andClosedYearEqualTo(yearBefore);
		closedBeforeList=closedDao.selectByExample(exampleBefore);
		}
		
		if(closedList.size()>0 || closedBeforeList.size()>0){
			return true;
		}
		return false;
	}

}
